<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>

            <el-form-item label="模块名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入模块名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"

                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"

                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"

                >删除
                </el-button>
            </el-col>
            <!--            <el-col :span="1.5">-->
            <!--                <el-button-->
            <!--                    type="warning"-->
            <!--                    plain-->
            <!--                    icon="el-icon-download"-->
            <!--                    size="mini"-->
            <!--                    @click="handleExport"-->
            <!--                    v-hasPermi="['module:fertilizerDistributorV1:export']"-->
            <!--                >导出-->
            <!--                </el-button>-->
            <!--            </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="fertilizerDistributorV1List" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="模块名称" align="center" prop="name"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"

                    >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"

                    >删除
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleAddress(scope.row)"

                    >地址
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleLog(scope.row)"

                    >历史记录
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleFertilizerOverview(scope.row)"

                    >通道概览
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleFertilizerMonitor(scope.row)"

                    >设备监控
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改施肥机v1对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                <el-form-item label="网关" prop="gatewayId">
                    <el-select v-model="form.gatewayId"
                               placeholder="请选择网关"
                    >
                        <el-option
                            v-for="item in gatewayListOption"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>


                <el-form-item label="模块名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入模块名称"/>
                </el-form-item>

                <el-form-item label="站号" prop="siteNumber">
                    <el-input-number v-model="form.siteNumber" :min="0"></el-input-number>
                </el-form-item>

                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addFertilizerDistributorV1,
    delFertilizerDistributorV1,
    getFertilizerDistributorV1,
    listFertilizerDistributorV1,
    updateFertilizerDistributorV1
} from '@/api/module/fertilizerDistributorV1'

import gatewaySelect from '@/views/common/GatewaySelect.vue'

export default {
    name: 'FertilizerDistributorV1',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 施肥机v1表格数据
            fertilizerDistributorV1List: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: '模块名称不能为空', trigger: 'blur' }
                ],
                gatewayId: [
                    { required: true, message: '请选择网关', trigger: 'blur' }
                ],
                siteNumber: [
                    { required: true, message: '站号不能为空', trigger: 'blur' }
                ]
            },
            // 用于修改回显所属网关
            gatewayListOption: []
        }
    },
    components: {
        gatewaySelect
    },
    created() {
        this.getList()
    },
    methods: {

        // 接收子组件传递的消息
        changeGateway(obj) {
            this.gatewayListOption = obj.gatewayListOption
            this.queryParams.gatewayId = obj.gatewayId
            this.getList()
        },

        getList() {
            this.loading = true
            listFertilizerDistributorV1(this.queryParams).then(response => {
                this.fertilizerDistributorV1List = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                name: null,
                params: null,
                isInner: '0',
                gatewayId: null,
                siteNumber: 1,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加施肥机v1'
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getFertilizerDistributorV1(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改施肥机v1'
            })
        },

        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateFertilizerDistributorV1(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addFertilizerDistributorV1(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除施肥机v1编号为"' + ids + '"的数据项？').then(function() {
                return delFertilizerDistributorV1(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('module/fertilizerDistributorV1/export', {
                ...this.queryParams
            }, `fertilizerDistributorV1_${new Date().getTime()}.xlsx`)
        },

        handleAddress(row) {
            let moduleId = row.id
            this.$router.push('/module/fertilizer/address/' + moduleId)
        },

        handleLog(row) {
            let moduleId = row.id
            this.$router.push('/module/fertilizer/log/' + moduleId)
        },
        handleFertilizerOverview(row) {
            let moduleId = row.id
            this.$router.push('/module/fertilizer/overview/' + moduleId)
        },
        handleFertilizerMonitor(row) {
            let moduleId = row.id
            this.$router.push('/module/fertilizer/monitor/' + moduleId)
            // this.$router.push({
            //     path: '/equipment/fertilizer',
            //     query: {
            //         moduleId: moduleId
            //     }
            // })
        }
    }
}
</script>
